package com.atguigu.jdbc.dao;

import com.atguigu.jdbc.bean.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * Date:2020/9/25
 * Author:ybc
 * Description:
 */
@Repository
public class UserDaoImpl {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void addUser(){
        String sql = "insert into t_user(username,password) values(?,?)";
        jdbcTemplate.update(sql, "admin", "123");
    }

    public List<User> getUserList(){
        String sql = "select * from t_user";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class));
    }

    public User getUserById(){
        String sql = "select * from t_user where id = ?";
        return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(User.class), 1);
    }

    public Integer getCount(){
        String sql = "select count(*) from t_user";
        return jdbcTemplate.queryForObject(sql, Integer.class);
    }

    public void addMoreUser(){
        String sql = "insert into t_user values(null,?,?,?,?,?)";
        List<Object[]> list = new ArrayList<>();
        list.add(new Object[]{"a1","11",10,"男","123@qq.com"});
        list.add(new Object[]{"a2","12",10,"男","123@qq.com"});
        list.add(new Object[]{"a3","13",10,"男","123@qq.com"});
        list.add(new Object[]{"a4","14",10,"男","123@qq.com"});
        jdbcTemplate.batchUpdate(sql, list);
    }

}
